package form;

import com.intellij.openapi.ui.Messages;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GeneratingUnitFilesDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK; // 确定
    private JButton buttonCancel; // 取消
    private JRadioButton mmRadioButton; // mm
    private JRadioButton dpRadioButton; // dp
    private JRadioButton ptRadioButton; // pt
    private JRadioButton inRadioButton; // in
    private JRadioButton a01RadioButton; // 0.1
    private JRadioButton a02RadioButton; // 0.2
    private JRadioButton a03RadioButton; // 0.3
    private JRadioButton a04RadioButton; // 0.4
    private JRadioButton a05RadioButton; // 0.5
    private JRadioButton a06RadioButton; // 0.6
    private JRadioButton a07RadioButton; // 0.7
    private JRadioButton a08RadioButton; // 0.8
    private JRadioButton a09RadioButton; // 0.9
    private JRadioButton a10RadioButton; // 1.0
    private JTextField a750TextField; // 最大值

    public GeneratingUnitFilesDialog(Callback callback) {
        this.callback = callback;
        setTitle("Generating Unit Files");
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        // setSize(500, 300);
        setLocationRelativeTo(null);

        buttonOK.addActionListener(e -> onOK());

        buttonCancel.addActionListener(e -> onCancel());

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        // 默认选中 MM单位
        updateUnit(mmRadioButton);
        JRadioButton[] units = {mmRadioButton, dpRadioButton, ptRadioButton, inRadioButton};
        for (JRadioButton btn : units) {
            btn.addActionListener(e -> updateUnit(btn));
        }
        // 默认选中生成0.5倍
        updateMultiple(a05RadioButton);
        JRadioButton[] multiples = {a01RadioButton, a02RadioButton, a03RadioButton,
                a04RadioButton, a05RadioButton, a06RadioButton,
                a07RadioButton, a08RadioButton, a09RadioButton, a10RadioButton};
        for (JRadioButton btn : multiples) {
            btn.addActionListener(e -> updateMultiple(btn));
        }
    }

    /**
     * 更新单位
     */
    private void updateUnit(JRadioButton btn) {
        setNotSelected(mmRadioButton, dpRadioButton, ptRadioButton, inRadioButton);
        btn.setSelected(true);
    }

    /**
     * 更新倍数
     */
    private void updateMultiple(JRadioButton btn) {
        setNotSelected(a01RadioButton, a02RadioButton, a03RadioButton,
                a04RadioButton, a05RadioButton, a06RadioButton,
                a07RadioButton, a08RadioButton, a09RadioButton, a10RadioButton);
        btn.setSelected(true);
    }

    /**
     * 设置不选中
     */
    private void setNotSelected(JRadioButton... btns) {
        int count = null == btns ? 0 : btns.length;
        for (int i = 0; i < count; i++) {
            btns[i].setSelected(false);
        }
    }

    private CodeUnit getCodeUnit() {
        if (mmRadioButton.isSelected()) {
            return CodeUnit.MM;
        }
        if (dpRadioButton.isSelected()) {
            return CodeUnit.DP;
        }
        if (ptRadioButton.isSelected()) {
            return CodeUnit.PT;
        }
        if (inRadioButton.isSelected()) {
            return CodeUnit.IN;
        }
        return CodeUnit.MM;
    }

    private Double getMultiple() {
        JRadioButton[] multiples = {a01RadioButton, a02RadioButton, a03RadioButton,
                a04RadioButton, a05RadioButton, a06RadioButton,
                a07RadioButton, a08RadioButton, a09RadioButton, a10RadioButton};
        for (JRadioButton btn : multiples) {
            if (btn.isSelected()) {
                try {
                    return Double.parseDouble(btn.getText().trim());
                } catch (Exception e) {
                    return 0.5d;
                }
            }
        }
        return 0.5d;
    }

    /**
     * 获取生成文件最大值
     */
    private Double getMaxValue() {
        try {
            return Double.parseDouble(a750TextField.getText().trim());
        } catch (Exception e) {
            return 750.0d;
        }
    }

    private void onOK() {
        if (callback != null) {
            callback.onOk(getCodeUnit(), getMultiple(), getMaxValue());
        }
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    private Callback callback;

    public interface Callback {
        void onOk(CodeUnit unit, Double multiple, Double maxValue);
    }

    public static void main(String[] args) {
        GeneratingUnitFilesDialog dialog = new GeneratingUnitFilesDialog((unit, multiple, maxValue) -> {
            Messages.showInfoMessage("unit => " + unit + "\n" +
                    "multiple => " + multiple + "\n" +
                    "maxValue => " + maxValue + "\n", "Success");
        });
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }
}